/**
 * @file       PagingByEditFieldStrategy.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Feb 2, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "PagingByEditFieldStrategy.h"
#include "errorcode.h"
#include "PageInfo.h"

namespace NS_LCD_AFW
{

CPagingByEditFieldStrategy::CPagingByEditFieldStrategy()
{
}

CPagingByEditFieldStrategy::~CPagingByEditFieldStrategy()
{
}

int CPagingByEditFieldStrategy::calculatePaging(
		const CFramePtrArray & framePtrArray, const CRect & parentRect,
		CPageInfo & pageResult)
{
	if (!parentRect.isValidRect())
	{
		return ERR_INVALID_RECT;
	}
	int rectLen = parentRect.getWidth() * parentRect.getHight();
	if (framePtrArray.size() == 0)
	{
		pageResult.setTotalPage(1);
	}
	else
	{
		(int) framePtrArray.size() % rectLen == 0 ?
				pageResult.setTotalPage((int) framePtrArray.size() / rectLen) :
				pageResult.setTotalPage(
						((int) framePtrArray.size() / rectLen) + 1);
	}
	for (int i = 0; i < pageResult.getTotalPage(); ++i)
	{
		int iEndIndex = (i + 1) * rectLen;
		if (iEndIndex > framePtrArray.size())
		{
			iEndIndex = framePtrArray.size();
		}
		pageResult.setPageInfo(i + 1, i * rectLen, iEndIndex);
	}

	return ERR_SUCCESS;
}

int CPagingByEditFieldStrategy::calculateViewPort(
		const CFramePtrArray & framePtrArray, const CRect & parentRect,
		CPageInfo & pageResult)
{
	int curPage = pageResult.getCurPage();
	int curHightlightObjectIndex = pageResult.getHightLightObjIndex();
	int nextHightlightObjectIndex = -1;

	switch (pageResult.getDirection())
	{
	case CPageInfo::UP:
	{
		if (curPage <= 1)
		{
			pageResult.setNeedSwitchPage(false);
			return ERR_NO_CHANGE;
		}
		else
		{
			pageResult.setCurPage(curPage - 1);
			pageResult.setNeedSwitchPage(true);
			pageResult.setHightLightObjIndex(
					findNextHightlightableObjectIndex(framePtrArray,
							pageResult.getEndIndex() - 1,
							pageResult.getStartIndex(),false));
		}
	}
		break;
	case CPageInfo::DOWN:
	{
		if (curPage >= pageResult.getTotalPage())
		{
			pageResult.setNeedSwitchPage(false);
			return ERR_NO_CHANGE;
		}
		else
		{
			pageResult.setCurPage(curPage + 1);
			pageResult.setNeedSwitchPage(true);
			pageResult.setHightLightObjIndex(
					findNextHightlightableObjectIndex(framePtrArray,
							pageResult.getStartIndex(),
							pageResult.getEndIndex(),true));
		}
	}
		break;
	case CPageInfo::LEFT:
	{
		nextHightlightObjectIndex = findNextHightlightableObjectIndex(
				framePtrArray, curHightlightObjectIndex - 1,
				pageResult.getStartIndex(),false);
		if (nextHightlightObjectIndex
				< 0 /*|| curHightlightObjectIndex == pageResult.getStartIndex()*/)
		{
			if (curPage <= 1)
			{
				pageResult.setNeedSwitchPage(false);
				pageResult.setHightLightObjIndex(curHightlightObjectIndex - 1);
				return ERR_NO_CHANGE;
			}
			else
			{
				curPage -= 1;
				pageResult.setCurPage(curPage);
				pageResult.setNeedSwitchPage(true);
				pageResult.setHightLightObjIndex(
						findNextHightlightableObjectIndex(framePtrArray,
								pageResult.getEndIndex(),
								pageResult.getStartIndex(),false));
			}
		}
		else
		{
			pageResult.setHightLightObjIndex(nextHightlightObjectIndex);
			pageResult.setNeedSwitchPage(false);
			return ERR_NO_CHANGE;
		}
	}
		break;
	case CPageInfo::RIGHT:
	{
		nextHightlightObjectIndex = findNextHightlightableObjectIndex(
				framePtrArray, curHightlightObjectIndex + 1,
				pageResult.getEndIndex(),true);
		if (nextHightlightObjectIndex
				< 0 /*|| curHightlightObjectIndex + 1 == pageResult.getEndIndex()*/)
		{
			if (curPage >= pageResult.getTotalPage())
			{
				pageResult.setNeedSwitchPage(false);
				pageResult.setHightLightObjIndex(curHightlightObjectIndex + 1);
				return ERR_NO_CHANGE;
			}
			else
			{
				curPage += 1;
				pageResult.setCurPage(curPage);
				pageResult.setNeedSwitchPage(true);

				pageResult.setHightLightObjIndex(
						findNextHightlightableObjectIndex(framePtrArray,
								pageResult.getStartIndex(),
								pageResult.getEndIndex(),true));
			}
		}
		else
		{
			pageResult.setHightLightObjIndex(nextHightlightObjectIndex);
			pageResult.setNeedSwitchPage(false);
			return ERR_NO_CHANGE;
		}
	}
		break;
	case CPageInfo::UNKNOW:
	{
		pageResult.setCurPage(curPage);
		pageResult.setNeedSwitchPage(true);

		pageResult.setHightLightObjIndex(
				findNextHightlightableObjectIndex(framePtrArray,
						pageResult.getStartIndex(), pageResult.getEndIndex(),true));
	}
		break;
	default:
		pageResult.setNeedSwitchPage(false);
		return ERR_NO_CHANGE;
		break;
	}

	return ERR_SUCCESS;
}

} /* namespace NS_LCD_AFW */
